package com.pzr.soccer.match.controller;

import com.pzr.soccer.common.aop.MetricTime;
import com.pzr.soccer.match.dao.GameRepository;
import com.pzr.soccer.match.domain.Game;
import com.pzr.soccer.match.domain.MatchInfo;
import com.pzr.soccer.match.model.GameWrapper;
import com.pzr.soccer.match.model.request.DateLT;
import com.pzr.soccer.match.model.request.SummaryRequestBox;
import com.pzr.soccer.match.service.GameService;
import com.pzr.soccer.model.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author pzr
 * @date:2021-12-15-18:10
 * @Description:
 **/
@RestController
@RequestMapping("/game")
public class GameController {
    @Autowired
    GameService service;
    @Autowired
    GameRepository repository;
    @Autowired
    MongoOperations mongoOperations;

    @GetMapping("/todayOnGoing")
    public List<GameWrapper> todayOnGoing() {
        return repository.findOnGoingGamesInRedis();
    }

    @GetMapping("/todayComing")
    public List<GameWrapper> todayComing() {
        return repository.findComingGamesInRedis();
    }

    @GetMapping("/todayEnded")
    public List<GameWrapper> todayEnded() {
        return repository.findEndedGamesInRedis();
    }

    @GetMapping("/byDateLT")
    public List<String> getByDateLT(@RequestBody DateLT dlt) {

        return service.findByDateLT(dlt).stream().map(Game::getId).collect(Collectors.toList());
    }

    @GetMapping("/covertOld2newGame")
    @MetricTime("convert to new games")
    public R<Object> covertOld2newGame(@PathParam("total") int total) {


        int page = 0;
        int pageSize = 500;
        while (page * pageSize <= total) {
            Query query = new Query();
            query.skip(page * pageSize);
           Sort sort=Sort.by(Sort.Direction.DESC,"date");
           query.with(sort);
            if (page * pageSize + pageSize > total) {
                query.limit(total - page * pageSize);
            }else {

                query.limit(pageSize);
            }

            List<Game> games = service.covertOldFormatGame2New(mongoOperations.find(query, MatchInfo.class));
            mongoOperations.insertAll(games);
            System.out.println(page*pageSize+"/"+total);
            page++;

        }
        return R.ok("插入成功!");
    }



    @GetMapping("/summary")
    @MetricTime("summary")
    public R<Object> summary(@RequestBody SummaryRequestBox box){
        List<Game> byDateLT = service.findByDateLT(box.getDateLT());
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Integer> game_result_map=new HashMap<>();
        List<Game> selectByPreCondition = service.selectMatchIndex(box.getPreconditions(), byDateLT);

        //符合条件全部
        selectByPreCondition.forEach(game -> game_result_map.put(game.getId(),0));
        List<Game> games = service.selectMatchResult(box.getResultConditions(), selectByPreCondition);

        //符合结果的，覆盖符合条件的
        games.forEach(game -> game_result_map.put(game.getId(),1));

        resultMap.put("gameId_result",game_result_map);
        resultMap.put("total",byDateLT.size());
        resultMap.put("matchPreconditions",selectByPreCondition.size());
        resultMap.put("matchResultConditions",games.size());

        return R.ok(resultMap,"成功获取统计数据！");
    }


    @GetMapping("/getByIds")
    public R<Object> getByIds(@RequestBody List<String> gameIds){
        List<Game> games = mongoOperations.find(Query.query(Criteria.where("id").in(gameIds)), Game.class);
        return R.ok(games,"fetch games successfully!");
    }
}
